import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import "package:get/get.dart";
import 'package:http/http.dart' as http;
import 'package:osg/controllers/my_method.dart';

// final bsurl = 'api.gonghai.top';
// final upIMG = 'https://api.gonghai.top'; 
// final weburl = 'https://osg.gonghai.top';//上传


const bool isProduction = bool.fromEnvironment('dart.vm.product');





// final bsurl = 'yuanshen1.wexiang.vip';
// final upIMG = 'http://yuanshen1.wexiang.vip';

 

class HTTPS extends GetxController {
 String upIMG ='';
   String bsurl ='';
  String weburl ='';


  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();



       if (isProduction) {

      bsurl = 'api.gonghai.top';
     upIMG = 'https://api.gonghai.top'; 
     weburl = 'https://osg.gonghai.top';

    print('生产环境');
  } else {
    print('开发环境');
    

     bsurl = 'osg1.wexiang.vip';
     upIMG = 'https://osg1.wexiang.vip';
     weburl = 'http://osg4.wexiang.vip';


  }

    bsurl = 'osg1.wexiang.vip';
    upIMG = 'https://osg1.wexiang.vip';
    weburl = 'http://osg4.wexiang.vip';

    // bsurl = 'api.gonghai.top';
    // upIMG = 'https://api.gonghai.top'; 
    // weburl = 'https://osg.gonghai.top';
     

    Get.find<MyMethod>().myset('upIMG', upIMG);
    Get.find<MyMethod>().myset('weburl', weburl);
  }


  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  setWeb() {
    update();
  }

  Future getData(String url, Map<String, dynamic> data, [String? type]) async {
    final urls = Uri.https(bsurl, url, data);

    // final urls = 'http://${bsurl}${url}';

    loading();
    final response = await http.get(
      urls,
      headers: <String, String>{
        'Authorization': Get.find<MyMethod>().myget('token') ?? '',
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
    );

    return getdata(response, type);
  }

  Future nogetData(String url, Map<String, dynamic> data, [String? type]) async {
    final urls = Uri.https(bsurl, url, data);

    // final urls = 'http://${bsurl}${url}';

    final response = await http.get(
      urls,
      headers: <String, String>{
        'Authorization': Get.find<MyMethod>().myget('token') ?? '',
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
    );

    return nogetdata(response, type);
  }

  postData(url, data,[String? mtype]) async {
    final urls = Uri.https(bsurl, url);
    loading();
    final response = await http.post(
      urls,
      headers: <String, String>{
        'Authorization': Get.find<MyMethod>().myget('token') ?? '',
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
      body: jsonEncode(data),
    );
      if(mtype=='dianjing'){
         return jinggetdata(response);

      }else{
         return getdata(response);
      }
  }

  nopostData(url, data) async {
    final urls = Uri.https(bsurl, url);
    final response = await http.post(
      urls,
      headers: <String, String>{
        'Authorization': Get.find<MyMethod>().myget('token') ?? '',
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
      body: jsonEncode(data),
    );

    return nogetdata(response);
  }

nogetdata(response, [String? mtype]) {
    var res = jsonDecode(response.body);
    if (response.statusCode == 200) {
      // 如果请求成功，解析响应体
      if (res['code'] == 0) {
        return res['data'];
      } else {
        if (response.statusCode == 401) {
          Get.offNamed('/login/login_type');
        } else {
          showTost(res['message'] ?? res['error']);
        }
      }
    } else {
      if (response.statusCode == 401) {
        Get.offNamed('/login/login_type');
      } else {
        if (mtype == 'noload') {
          return res;
        } else {
          showTost(res['message'] ?? res['error']);
        }
        return null;
      }
    }
  }



jinggetdata(response, [String? mtype]) {
    Get.back();
    var res = jsonDecode(response.body);

    if (response.statusCode == 200) {
      // 如果请求成功，解析响应体
      if (res['code'] == 0) {
        return res;
      } else {
        if (response.statusCode == 401) {
          Get.offNamed('/login/login_type');
        } else {
          showTost(res['message'] ?? res['error']);
        }
      }
    } else {
      if (response.statusCode == 401) {
        Get.offNamed('/login/login_type');
      } else {
        if (mtype == 'noload') {
          return res;
        } else {
          showTost(res['message'] ?? res['error']);
        }
        return null;
      }
    }
  }



  getdata(response, [String? mtype]) {
    Get.back();
    var res = jsonDecode(response.body);

    if (response.statusCode == 200) {
      // 如果请求成功，解析响应体
      if (res['code'] == 0) {
        return res['data'];
      } else {
        if (response.statusCode == 401) {
          Get.offNamed('/login/login_type');
        } else {
          showTost(res['message'] ?? res['error']);
        }
      }
    } else {
      if (response.statusCode == 401) {
        Get.offNamed('/login/login_type');
      } else {
        if (mtype == 'noload') {
          return res;
        } else {
          showTost(res['message'] ?? res['error']);
        }
        return null;
      }
    }
  }
  

  loading() {
    Get.dialog(
      barrierDismissible:false,
      barrierColor: Colors.transparent,
      Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
              width: 100,
              height: 100,
              decoration: BoxDecoration(
                  color: Color(0X83000000),
                  borderRadius: BorderRadius.circular(15)),
              child: Center(
                  child: CircularProgressIndicator(
                      backgroundColor: Color(0XFFffffff))))
        ],
      ),
    );
  }

  showTost(text) {

Timer(Duration(milliseconds: 60), () {
    Get.dialog(
      barrierColor: Colors.transparent,
      AlertDialog(
        backgroundColor: Colors.transparent,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
        content: Container(
          alignment: Alignment.center,
              constraints: BoxConstraints(maxWidth: 300,maxHeight: 100),
              padding: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
              decoration: BoxDecoration(
                  color: Color(0X83000000),
                  borderRadius: BorderRadius.circular(15)),
              child: Text(text,
                  style: TextStyle(fontSize: 16, color: Color(0XFFFFFFFF))),
            )
        
        //  Row(
        //   mainAxisAlignment: MainAxisAlignment.center,
        //   children: [
           
        //   ],
        // ),
      ),
    );
});
   

    Future.delayed(Duration(seconds: 1), () {
      if (Get.isDialogOpen!) {
        // 确保对话框仍然打开
        Get.back(); // 关闭对话框
      }
    });
  }
}
